package cn.tinlan.shopcoin.app.koi;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallClazzProxy;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.subsciber.BaseSubscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.Utils.ToastUtils;
import cn.tinlan.shopcoin.app.koi.adapter.NotiveAdapter;
import cn.tinlan.shopcoin.app.koi.model.KoiNoticeModel;
import cn.tinlan.shopcoin.app.web.WebActivity;
import cn.tinlan.shopcoin.base.BaseActivity;
import cn.tinlan.shopcoin.base.CustomApiResult;
import cn.tinlan.shopcoin.widget.category.Constants;
import io.reactivex.Observable;

/**
 * Created by Mortimer on 2018/12/24. 09:42
 * 佛祖🙏，永无bug。
 */
public class KoiNoticeActivity extends BaseActivity {

    @BindView(R.id.koi_notice_title)
    TitleBar koi_noticeTitle;
    @BindView(R.id.koi_notice_refresh)
    SmartRefreshLayout koi_noticeRefresh;
    @BindView(R.id.koi_notice_rv)
    RecyclerView koi_noticeRV;

    private NotiveAdapter noticeAdapter;
    private List<KoiNoticeModel> KoiNoticeModelList;
    private int page = 1;
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_koi_notice;
    }

    @Override
    public void initView() {
        super.initView();

        drawableDefaultBar();


        initTitleBar();
        initRefresh();
        initNoticeRV();
    }

    private void initNoticeRV() {
        if (noticeAdapter == null) {
            LinearLayoutManager llm = new LinearLayoutManager(KoiNoticeActivity.this, LinearLayoutManager.VERTICAL, false);
            koi_noticeRV.setLayoutManager(llm);
            KoiNoticeModelList = new ArrayList<>();

            noticeAdapter = new NotiveAdapter(R.layout.view_koi_noticeitem,KoiNoticeModelList);


            koi_noticeRV.setAdapter(noticeAdapter);
        }else {
            noticeAdapter.notifyDataSetChanged();
        }

        noticeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                Intent intent = new Intent(KoiNoticeActivity.this, WebActivity.class);
                intent.putExtra("title",KoiNoticeModelList.get(position).getTitle());
                intent.putExtra("type",2);
                intent.putExtra("webCode",KoiNoticeModelList.get(position).getContent());
                startActivity(intent);
            }
        });
    }

    private void initRefresh() {
        koi_noticeRefresh.setRefreshHeader(new ClassicsHeader(this));
        koi_noticeRefresh.setRefreshFooter(new ClassicsFooter(this));
        koi_noticeRefresh.setEnableScrollContentWhenLoaded(true);//是否在加载完成时滚动列表显示新的内容
        koi_noticeRefresh.setEnableFooterFollowWhenLoadFinished(true);
        koi_noticeRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                loadNoticeData(page);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                loadNoticeData(page);
            }
        });
    }

    private void initTitleBar() {
        koi_noticeTitle.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });
    }

    @Override
    public void initData() {
        super.initData();

        page = 1;
        loadNoticeData(page);
    }

    private void loadNoticeData(final int page1) {

        Observable<List<KoiNoticeModel>> observable = EasyHttp.get(Constants.KoiNoticeList)
                .params("page",page1+"")
                .execute(new CallClazzProxy<CustomApiResult<List<KoiNoticeModel>>, List<KoiNoticeModel>>(new TypeToken<List<KoiNoticeModel>>() {}.getType()) {
                });

        observable.subscribe(new BaseSubscriber<List<KoiNoticeModel>>() {
            @Override
            public void onError(ApiException e) {

                koi_noticeRefresh.finishRefresh();
                koi_noticeRefresh.finishLoadMore();
                if (page > 1){
                    page--;
                }else {
                    page = 1;
                }

//                ToastUtils.show("数据请求失败");

            }

            @Override
            public void onNext(List<KoiNoticeModel> KoiNoticeModels) {
                super.onNext(KoiNoticeModels);

                if (KoiNoticeModels.size() < 1){
                    ToastUtils.show("数据暂无");
                    return;
                }

                if (page1 == 1){
                    KoiNoticeModelList = KoiNoticeModels;
                    noticeAdapter.setNewData(KoiNoticeModelList);
                    koi_noticeRefresh.finishRefresh();
//                    mShopListAdapter.notifyDataSetChanged();
                }else {
                    noticeAdapter.addData(KoiNoticeModels);
                    if (KoiNoticeModels.size() < 16) {
                        koi_noticeRefresh.finishLoadMoreWithNoMoreData();  //全部加载完成,没有数据了调用此方法
                    }else {
                        koi_noticeRefresh.finishLoadMore();      //加载完成
                    }

//
                }

            }
        });
    }


}
